#!/usr/bin/perl

do '../web-lib.pl';

use Data::Dumper;
&init_config();

#list_filesystems(void)
#return @list each element is point to @f
#@f = (dir, lable, ext3.., rw, g_u_num, action)
sub list_filesystems()
{
#	my (@list, @listmount, $i, $j, $k, @rtlist, $mykey, @voltab);
#	my (@mountline, @voltabline, $ugmode, $action, @rtline);
#	&foreign_require("quota", "quota-lib.pl");
#	@list = &foreign_call("quota", "list_filesystems");

#	return @list;

	my (@list, @listmount, $i, $j, $k, @rtlist, $mykey, @voltab);
	my (@mountline, @voltabline, $ugmode, $action, @rtline, $volpath);
	&foreign_require("quota", "quota-lib.pl");

	#list mounted
	#open(MOUNTLIST, "$config{'mount'}|");
	#@listmount = <MOUNTLIST>;
	#close(MOUNTLIST);
	
	@listmount = `$config{'mount'} 2>/dev/null`;

	open(VOLTAB, "$config{'voltab'}");
	@voltab = <VOLTAB>;
	close(VOLTAB);

	$i = 0; $j = 0; $k = 0;
	while($i < @listmount)
	{
		$listmount[$i] =~ s/#.*$//;
		chomp($listmount[$i]);
		@mountline = split(/\s+/, $listmount[$i]);
		$j = 0;
		while($j < @voltab)
		{
			$voltab[$j] =~ s/#.*$//;
			chomp($voltab[$j]);
			@voltabline = split(/\s+/, $voltab[$j]);
			$volpath = '/usrvol/'.$voltabline[0];
			if((@mountline[2] eq $volpath) && ($voltabline[2] eq "normal"))
			{
				$mountline[5] =~ s/\(//;
				$mountline[5] =~ s/\)//;
				$ugmode = ($mountline[5] =~ /usrquota/ ? 1 : 0) + ($mountline[5] =~ /grpquota/ ? 2 : 0);
				@rtline = ($mountline[2], "$voltabline[0]", $mountline[4], $mountline[5], $ugmode);
				&foreign_require("quota", "redhat-linux-lib.pl");
				#my $out = `$config{'command_quotaon_ug'} $mountline[2] 2>&1`;
				my $out = &ez_system12(split(/\s/, $config{'command_quotaon_ug'}), $mountline[2]);
				@rtline[5] = &foreign_call("quota", "quota_now", \@rtline);
				$rtlist[$k] = [@rtline];
				#$rtlist[$k] = [($mountline[2], "LABLE=$voltabline[0]", $mountline[4], $mountline[5])];
				$k++;
			}
			$j++;
		}
		$i++;
	}
	return @rtlist;

}

#void sync_olusr_with_vols(void)
sub sync_olusr_with_vols()
{
	my (@flist, $i);
	@flist = &list_filesystems();
}

#void sync_olusr_with_voldir($dir)
sub sync_olusr_with_voldir()
{
	my ($dir, %user, $i);
	$n = &repquotauser(\%user, $dir);
}

#get_df_detail(void)
#return @df_list
sub get_df_detail()
{
	my (@rt_list);

	#open(DF, "$config{'df'}|");
	#@rt_list = <DF>;
	#close(DF);

	@rt_list = `$config{'df'} 2>/dev/null`;

	return @rt_list;
}

#block_to_k(blocknum)
#return $knum;
sub block_to_k()
{
	my ($blocknum, $k);

	$blocknum = @_[0];

	$k = $blocknum;
	return $k;
}

#blocks_to_K_num($blocknum)
#return $kstr.    kstr=345k
sub blocks_to_K_str()
{
	my ($blocknum, @kstr);
	$blocknum = @_[0];

	$blocknum = &block_to_k($blocknum);
	@kstr = ("$blocknum","KB", "integer");

	return @kstr;
}

#blocks_to_M_str($blocknum)
#return @mstr;   @mstr = ($mnum, "M")
sub blocks_to_M_str()
{
	my ($blocknum, @mstr, $is_float);

	$blocknum = @_[0];

	$blocknum = &block_to_k($blocknum);
	$is_float = $blocknum % 1024;
	$blocknum = $blocknum / 1024;

	if($is_float != 0)
	{
		@mstr = ("$blocknum","MB", "float");
	}
	else
	{
		@mstr = ("$blocknum","MB", "integer");
	}

	return @mstr;
}

#blocks_to_G_str($blocknum)
#return @gstr;   @gstr = ($gnum, "G")
sub blocks_to_G_str()
{
	my ($blocknum, @gstr, $is_float);

	$blocknum = @_[0];
	$blocknum = &block_to_k($blocknum);
	$is_float = $blocknum % (1024 * 1024);
	$blocknum = $blocknum / (1024 * 1024);

	if($is_float != 0)
	{
		@gstr = ("$blocknum","GB", "float");
	}
	else
	{
		@gstr = ("$blocknum","GB", "integer");
	}
	#printf $gstr, "%f", $blocknum;

	#$gstr = $gstr."G";
	return @gstr;
}

#blocks_to_KMG_str($blocknum)
#return @kmgstr;   @kmgstr = ($kmgnum, "KB|MB|GB", "integer|float")
sub blocks_to_KMG_str()
{
	my ($blocknum, @kmgstr);
	$blocknum = @_[0];

	if($blocknum >= (1024 * 1024))
	{
		@kmgstr = &blocks_to_G_str($blocknum);
	}
	elsif($blocknum >= 1024)
	{
		@kmgstr = &blocks_to_M_str($blocknum);
	}
	else
	{
		@kmgstr = &blocks_to_K_str($blocknum);
	}

	return @kmgstr;
}

#knum_to_block($knum)
#return blocknum
sub knum_to_block()
{
	my ($blocknum, $knum);

	$knum = @_[0];
	$blocknum = $knum;
	return $blocknum;
}

#KMG_str_to_blocks($kmg_num, $kmg_unit)
#return blocknum
sub KMG_str_to_blocks()
{
	my ($blocknum, $kmg_num, $kmg_unit);
	$kmg_num = @_[0];
	$kmg_unit = @_[1];

	if($kmg_unit eq "KB")
	{
		return &knum_to_block($kmg_num);
	}
	elsif($kmg_unit eq "MB")
	{
		return &knum_to_block($kmg_num * 1024);
	}
	elsif($kmg_unit eq "GB")
	{
		return &knum_to_block($kmg_num * 1024 * 1024);
	}
	elsif($kmg_unit eq "TB")
	{
		return &knum_to_block($kmg_num * 1024 * 1024 * 1024);
	}
}

#repquotauser(\%user, $dir)
#user in redhat
sub repquotauser()
{
	my ($pt_user, $dir, $i, $j, @line, @rep_user, %displayuser, @localuser, @pdcuser, $bgrace);
	my ($hblocks, $sblocks, $ublocks, $hfiles, $sfiles, $ufiles);

	$pt_user = @_[0];
	$dir = @_[1];

	my $err_vol;

#	@rep_user = `repquota -v $dir 2>/dev/null`;
	$out = &ez_system12("repquota",  "-v", $dir);
	@rep_user = split("\n", $out);

	open(LOCALUSER, "$config{'localuser'}");
	@localuser = <LOCALUSER>;
	close(LOCALUSER);	

	open(PDCUSER, "$config{'pdcuser'}");
	@pdcuser = <PDCUSER>;
	close(PDCUSER);

	$i = 0;
	while($i < @localuser)
	{
		$localuser[$i] =~ s/#.*$//;
		chomp($localuser[$i]);
		@line = split(":", $localuser[$i]);
		if($line[0])
		{
			$displayuser{$line[0]} = "yes";
		}
		$i++;
	}

	$i = 0;
	while($i < @pdcuser)
	{
		$pdcuser[$i] =~ s/#.$//;
		chomp($pdcuser[$i]);
		if($pdcuser[$i])
		{
			$pdcuser[$i] = lc($pdcuser[$i]);
			$displayuser{$pdcuser[$i]} = "yes";
		}
		$i++;
	}

	$i = 0;
	$j = 0;
	while($i < @rep_user)
	{
		if($rep_user[$i] =~ /.*\s+(\d+)\s+(\d+)\s+(\d+)\s+(.*)\s+(\d+)\s+(\d+)\s+(\d+)\s*(.\w+)*/)
		{
			$ublocks = $1;
			$sblocks = $2;
			$hblocks = $3;
			$bgrace = $4;
			$ufiles = $5;
			$sfiles = $6;
			$hfiles = $7;
		
			@line = split(/\s+/, $rep_user[$i]);
			if($line[5] =~ /^\d+$/)
			{
				$bgrace = "";
			}
			else
			{
				$bgrace = $line[5];
			}

			if($bgrace eq "none")
			{
				$bgrace = "";
			}

			#print "<p>$line[0], $ublocks, $sblocks, $hblocks, $bgrace, $ufiles, $sfiles, $hfiles \n";
			if($line[0] =~ /.*\\.*/)
			{
				$line[0] = lc($line[0]);
			}
			if($displayuser{$line[0]})
			{
				$pt_user->{"$j^user"} = $line[0];
				$pt_user->{"$j^ublocks"} = $ublocks;
				$pt_user->{"$j^sblocks"} = $sblocks;
				$pt_user->{"$j^hblocks"} = $hblocks;
				$pt_user->{"$j^ufiles"} = $ufiles;
				$pt_user->{"$j^sfiles"} = $sfiles;
				$pt_user->{"$j^hfiles"} = $hfiles;
				$pt_user->{"$j^bgrace"} = $bgrace;
				
				if((sblocks eq 0) && (hblocks eq 0))
				{
					$pt_user->{"$j^status"} = $text{"status_limit_no"};
				}
				elsif((hblocks <= ublocks) && (hblocks > 0))
				{
					$pt_user->{"$j^status"} = $text{"status_limit_exceed"};
				}
				elsif((sblocks <= ublocks) && (sblocks > 0))
				{
					$pt_user->{"$j^status"} = $text{"status_limit_p_warning"};
				}
				else
				{
					$pt_user->{"$j^status"} = $text{"status_limit_ok"};
				}
#				if(($sblocks == 0) && ($hblocks == 0))
#				{
#					$pt_user->{"$j^status"} = $text{'status_limit_no'};
#				}
#				else
#				{
#					$pt_user->{"$j^status"} = $text{'status_limit_ok'};
#				}
				$j++;
			}
		
		}
		$i++;
	}
	return $j;
}

#reppdcquota(\%pdcuser, $dir)
sub reppdcquota()
{
	my ($pdcuser, $dir, %user, $n, @pdcuserfie, %ispdcuser, $i, $index, $wbstatus);
	$pdcuser = @_[0];
	$dir = @_[1];

	$wbstatus = get_winbind_status();
	if($wbstatus eq "stopped")
	{
		return 0;
	}

	$n = &repquotauser(\%user, $dir);
	@pdcuserfile = &get_is_pdcuser(\%ispdcuser);

	for($i = 0; $i < $n; $i++)
	{
		if($ispdcuser{$user{$i.'^user'}})
		{
			$ispdcuser{$user{$i.'^user'}} = "$i";
		}
	}

	$n = 0;

	for($i = 0; $i < @pdcuserfile; $i++)
	{
		if($ispdcuser{$pdcuserfile[$i]} eq "yes")
		{
			#print "<p>", $pdcuserfile[$i];
			$pdcuser->{"$i^user"} = $pdcuserfile[$i];
			$pdcuser->{"$i^ublocks"} = 0;
			$pdcuser->{"$i^sblocks"} = 0;
			$pdcuser->{"$i^hblocks"} = 0;
			$pdcuser->{"$i^ufiles"} = 0;
			$pdcuser->{"$i^sfiles"} = 0;
			$pdcuser->{"$i^hfiles"} = 0;
			$pdcuser->{"$i^bgrace"} = "";
			$pdcuser->{"$i^status"} = $text{'status_limit_no'};
			$n++;
		}
		elsif($ispdcuser{$pdcuserfile[$i]} >= 0)
		{
			$index = $ispdcuser{$pdcuserfile[$i]};

			$pdcuser->{"$i^user"} = $pdcuserfile[$i];
			$pdcuser->{"$i^ublocks"} = $user{$index.'^ublocks'};
			$pdcuser->{"$i^sblocks"} = $user{$index.'^sblocks'};
			$pdcuser->{"$i^hblocks"} = $user{$index.'^hblocks'};
			$pdcuser->{"$i^ufiles"} = $user{$index.'^ufiles'};
			$pdcuser->{"$i^sfiles"} = $user{$index.'^sfiles'};
			$pdcuser->{"$i^hfiles"} = $user{$index.'^hfiles'};
			$pdcuser->{"$i^bgrace"} = $user{$index.'^bgrace'};

			if(($pdcuser->{"$i^sblocks"} eq 0) && ($pdcuser->{"$i^hblocks"} eq 0))
			{
				$pdcuser->{"$i^status"} = $text{"status_limit_no"};
			}
			elsif(($pdcuser->{"$i^hblocks"} <= ublocks) && ($pdcuser->{"$i^hblocks"} > 0))
			{
				$pdcuser->{"$i^status"} = $text{"status_limit_exceed"};
			}
			elsif(($pdcuser->{"$i^sblocks"} <= ublocks) && ($pdcuser->{"$i^sblocks"} > 0))
			{
				$pdcuser->{"$i^status"} = $text{"status_limit_p_warning"};
			}
			else
			{
				$pdcuser->{"$i^status"} = $text{"status_limit_ok"};
			}

#			if(($pdcuser->{"$i^sblocks" == 0}) && ($pdcuser->{"$i^hblocks"} == 0))
#			{
#				$pdcuser->{"$i^status"} = $text{'status_limit_no'};
#			}
#			else
#			{
#				$pdcuser->{"$i^status"} = $text{'status_limit_ok'};
#			}
			$n++;
		}
	}
	return $n;
}

#replocalquota(\%localuser, $dir)
sub replocalquota()
{
	my ($localuser, $dir, %user, $n, @localuserlist, %is_localuser, $i, $index, $rt_leng);

	$localuser = @_[0];
	$dir = @_[1];

	@localuserlist = get_is_localuser(\%is_localuser);
	#print $localuserlist[0];
	$n = &repquotauser(\%user, $dir);

	for($i = 0; $i < $n; $i++)
	{
		if($is_localuser{$user{$i.'^user'}})
		{
			$is_localuser{$user{$i.'^user'}} = "$i";
		}
	}


	$n = 0;

	for($i = 0; $i < @localuserlist ;$i++)
	{
		if($is_localuser{$localuserlist[$i]} eq "yes")
		{
			#print "<p>", $pdcuserfile[$i];
			$localuser->{"$i^user"} = $localuserlist[$i];
			$localuser->{"$i^ublocks"} = 0;
			$localuser->{"$i^sblocks"} = 0;
			$localuser->{"$i^hblocks"} = 0;
			$localuser->{"$i^ufiles"} = 0;
			$localuser->{"$i^sfiles"} = 0;
			$localuser->{"$i^hfiles"} = 0;
			$localuser->{"$i^bgrace"} = "";
			$localuser->{"$i^status"} = $text{'status_limit_no'};
			$n++;
		}
		elsif($is_localuser{$localuserlist[$i]} >= 0)
		{
			$index = $is_localuser{$localuserlist[$i]};

			$localuser->{"$i^user"} = $localuserlist[$i];
			$localuser->{"$i^ublocks"} = $user{$index.'^ublocks'};
			$localuser->{"$i^sblocks"} = $user{$index.'^sblocks'};
			$localuser->{"$i^hblocks"} = $user{$index.'^hblocks'};
			$localuser->{"$i^ufiles"} = $user{$index.'^ufiles'};
			$localuser->{"$i^sfiles"} = $user{$index.'^sfiles'};
			$localuser->{"$i^hfiles"} = $user{$index.'^hfiles'};
			$localuser->{"$i^bgrace"} = $user{$index.'^bgrace'};

			if(($localuser->{"$i^sblocks"} eq 0) && ($localuser->{"$i^hblocks"} eq 0))
			{
				$localuser->{"$i^status"} = $text{"status_limit_no"};
			}
			elsif(($localuser->{"$i^hblocks"} <= ublocks) && ($localuser->{"$i^hblocks"} > 0))
			{
				$localuser->{"$i^status"} = $text{"status_limit_exceed"};
			}
			elsif(($localuser->{"$i^sblocks"} <= ublocks) && ($localuser->{"$i^sblocks"} > 0))
			{
				$localuser->{"$i^status"} = $text{"status_limit_p_warning"};
			}
			else
			{
				$localuser->{"$i^status"} = $text{"status_limit_ok"};
			}

#			if(($localuser->{"$i^sblocks" == 0}) && ($localuser->{"$i^hblocks"} == 0))
#			{
#				$localuser->{"$i^status"} = $text{'status_limit_no'};
#			}
#			else
#			{
#				$localuser->{"$i^status"} = $text{'status_limit_ok'};
#			}
			$n++;
		}
	}
	return $n;
}

sub get_winbind_status()
{
	my ($out, $rt_status);
	$out = `$config{'winbind_status'} 2>/dev/null`;
	if($out =~ /running/)
	{
		return "running";
	}
	return "stopped";
}

#check_input_num($input_num)
sub check_input_num()
{
	my ($input_num, $error_msg,$bignum, $smallnum, $str_length, $units);

	$input_num = @_[0];
	$units = @_[1];

	if($input_num =~ /^(\d+)(.*)/)
	{
		$bignum = $1;		
		if($2)
		{
			$smallnum = $2;
			if($smallnum =~ /^\.(\d+)(.*)/)
			{
				if($2)
				{
					$error_msg = $text{"error_msg_input_not_num"};					
				}
				else
				{
					$error_msg = "";

					$str_length = length($bignum);

					if($units eq "KB")
					{
						if($str_length > $config{'max_int_length'})
						{
							$error_msg = $text{'error_msg_input_num_tobig'};
						}						
					}
					
					if($units eq "MB")
					{
						if($str_length > 6)
						{
							$error_msg = $text{'error_msg_input_num_tobig'};
						}
					}
					
					if($units eq "GB")
					{
						if($str_length > 3)
						{
							$error_msg = $text{'error_msg_input_num_tobig'};
						}
					}					
				}
			}
			else
			{
				$error_msg = $text{"error_msg_input_not_num"};
			}
		}
		else
		{
			$error_msg = "";
			$str_length = length($input_num);

			if($units eq "KB")
			{
				if($str_length > $config{'max_int_length'})
				{
					$error_msg = $text{'error_msg_input_num_tobig'};
				}						
			}
			
			if($units eq "MB")
			{
				if($str_length > 6)
				{
					$error_msg = $text{'error_msg_input_num_tobig'};
				}
			}
			
			if($units eq "GB")
			{
				if($str_length > 3)
				{
					$error_msg = $text{'error_msg_input_num_tobig'};
				}
			}			
		}
	}
	else
	{
		$error_msg = $text{"error_msg_input_not_num"};
	}
	return $error_msg;
}

#check_blocks_input($h_blocks, $s_blocks)
sub check_blocks_input()
{
	my ($s_blocks, $h_blocks, $error_msg);
	$h_blocks = @_[0];
	$s_blocks = @_[1];

	$error_msg = "";

	if($s_blocks > $h_blocks)
	{
		$error_msg = $text{"error_msg_sblock_bigto_hblock"};
	}
	return $error_msg;
}

#check input_not_integer(inputnum)
sub check_input_not_integer()
{
	my ($input_num, $error_msg,$bignum, $smallnum, $str_length);

	$input_num = @_[0];

	if($input_num =~ /^(\d+)(.*)/)
	{
		$bignum = $1;
		if($2)
		{
			$error_msg = $text{"error_msg_input_not_integer"};
		}
		else
		{
			$error_msg = "";
			$str_length = length($input_num);
			if($str_length > $config{'max_int_length'})
			{
				$error_msg = $text{'error_msg_input_num_tobig'};
			}
		}
	}
	else
	{
		$error_msg = $text{"error_msg_input_not_num"};
	}
	return $error_msg;

}

sub sync_quota_mount()
{
	my (@list, @defaultquota, $i, %dq, @line, %mt, $dir, $sfiles);
	@list  = &list_filesystems();


	open(DEFAULTQUOTA, $config{"userdefaultquota"});
	@defaultquota = <DEFAULTQUOTA>;
	close(DEFAULTQUOTA);

	$i = 0;
	while($i < @defaultquota)
	{
		$defaultquota[$i] =~ s/#.*$//;
		@line = split(/\s+/, $defaultquota[$i]);
		if($line[0])
		{
			$dq{$line[0]} = join(" ", $line[1], $line[2], $line[3], $line[4]);
		}
		$i++;
	}
#(dir, lable, ext3.., rw, g_u_num, action)
	$i = 0;
	while($i < @list)
	{
		if(!$dq{$list[$i]->[0]})
		{
			#$sfiles = &get_largest_inodes_with_fs($list[$i]->[0]);
			$sfiles = 0;
			$dq{$list[$i]->[0]} = "0 0 $sfiles 0";
			#print "<p>", $list[$i]->[0];
		}
		if($list[$i]->[5] > 0)
		{
			$mt{$list[$i]->[0]} = "yes";
			#print "<p>", $list[$i]->[0];
		}
		$i++;
	}

	foreach $dir (keys %dq)
	{
		if(!$mt{$dir})
		{
			$dq{$dir} = "";
		}
	}

	&lock_file($config{"userdefaultquota"});

	open(DEFAULTQUOTA, ">$config{'userdefaultquota'}");
	foreach $dir (keys %dq)
	{
		if($dq{$dir})
		{
			print DEFAULTQUOTA $dir." ".$dq{$dir}."\n";
			#print "<p>", $dir." ".$dq{$dir}."\n";
		}
	}
	close(DEFAULTQUOTA);

	&unlock_file($config{"userdefaultquota"});
}

#get_default_quota(\%defquota)
sub get_default_quota()
{
	my (@defquota, @line, $i, $rt_defquota);

	$rt_defquota = @_[0];

	open(DEFQUOTA, "$config{'userdefaultquota'}");
	@defquota = <DEFQUOTA>;
	close(DEFQUOTA);

	$i = 0;
	while($i < @defquota)
	{
		$defquota[$i] =~ s/#.*$//;
		chomp($defquota[$i]);
		@line = split(/\s+/, $defquota[$i]);
		if($line[0])
		{
			$rt_defquota->{$line[0]} = join(" ", $line[1], $line[2], $line[3], $line[4]);
		}
		$i++;
	}
}

#update_usrdef_quota($dir, sblocks, hblocks, sfiles, hfiles)
sub update_usrdef_quota()
{
	my (%defquota, $dir, $sblocks, $hblocks, $sfiles, $hfiles, $mykey);

	$dir = @_[0];
	$sblocks = @_[1];
	$hblocks = @_[2];
	$sfiles = @_[3];
	$hfiles= @_[4];

	&get_default_quota(\%defquota);

	$defquota{$dir} = join(" ", $sblocks, $hblocks, $sfiles, $hfiles);

	open(DEFQUOTA, ">$config{'userdefaultquota'}");
	foreach $mykey (keys %defquota)
	{
		print DEFQUOTA $mykey, " ", $defquota{$mykey}, "\n";
	}

	close(DEFQUOTA);
}

#get_largest_inodes_with_fs($dir)
sub get_largest_inodes_with_fs()
{
	my (@dflist, $i, @line, $rt_inodesnum, $dir);

	$dir = @_[0];
	$rt_inodesnum = 0;

	#open (DFFILE, "$config{'df'} -i|");
	#@dflist = <DFFILE>;
	#close (DFFILE);

	@dflist = `$config{'df'} 2>/dev/null`;

	$i = 0;
	while($i < @dflist)
	{
		@line = split(/\s+/, $dflist[$i]);
		if($line[5] eq $dir)
		{
			$rt_inodesnum = $line[1];
			last;
		}
		$i++;
	}
	return $rt_inodesnum;
}

sub get_is_pdcuser()
{
	my (@pdcuserfile, $pdcuser, $i);

	$pdcuser = @_[0];
	open(PDCUSER, $config{'pdcuser'});
	@pdcuserfile = <PDCUSER>;
	close(PDCUSER);

	$i = 0;
	while($i < @pdcuserfile)
	{
		$pdcuserfile[$i] =~ /#.*$/;
		chomp($pdcuserfile[$i]);
		if($pdcuserfile[$i])
		{
			$pdcuserfile[$i] = lc($pdcuserfile[$i]);
			$pdcuser->{$pdcuserfile[$i]}="yes";
		}
		$i++;
	}
	return @pdcuserfile;
}

#sub get_is_localuser(\%is_localuser)
#return @localuserlist;
sub get_is_localuser()
{
	my (@localuserfile, $is_localuser, $i, @line, @rt_list);

	$is_localuser = @_[0];
	open(LOCALUSER, $config{'localuser'});
	@localuserfile = <LOCALUSER>;
	close(LOCALUSER);


	$i = 0;
	while($i < @localuserfile)
	{
		$localuserfile[$i] =~ /#.*$/;
		@line = split(":", $localuserfile[$i]);
		if($line[0])
		{
			$line[0] = lc($line[0]);
			$is_localuser->{$line[0]}="yes";
		}
		$rt_list[$i] = $line[0];
		$i++;
	}
	return @rt_list;
}


#get_user_quota($name,$dir)
sub get_user_quota()
{
	my ($username, $dir, %user, $n, $i);
	$username = @_[0];
	$dir = @_[1];

	$n = &repquotauser(\%user, $dir);

	$i = 0;
	while($i < $n)
	{
		if($user{$i.'^user'} eq "$username")
		{			
			return ($user{$i.'^ublocks'}, $user{$i.'^sblocks'}, $user{$i.'^hblocks'}, $user{$i.'^ufiles', $user{$i.'^sfiles'}}, $user{$i.'^hfiles'});
		}
		$i++;
	}
}

#is_savequota_failed($username, $dir, $hblocks, $sblocks)
#return $error_msg
sub is_savequota_failed()
{
	my $error_msg, $set_hb, $set_sb, $username, $dir, @u_quot;

	$username = @_[0];
	$dir = @_[1];
	$set_hb = @_[2];
	$set_sb = @_[3];

	@u_quot = &get_user_quota($username, $dir);

	$error_msg = "";

	unless($u_quot[1])
	{
		$u_quot[1] = 0;
	}

	unless($u_quot[2])
	{
		$u_quot[2] = 0;
	}
	
	unless(($set_sb eq $u_quot[1]) && ($set_hb eq $u_quot[2]))
	{
		$error_msg = $text{'error_msg_savequota_failed_in_progress'};
	}
	
	return $error_msg;
}


#is_savegrace_failed($dir, $bt, $bu)
#return $error_msg
sub is_savegrace_failed()
{
	my $dir, $bt, $bu, @gr, $error_msg;

	$dir = @_[0];
	$bt = @_[1];
	$bu = @_[2];

	&foreign_require("quota", "quota-lib.pl");
	@gr = &foreign_call("quota", "get_user_grace", $dir);

	if(($bu == 0))
	{
		$bt = $bt / 60;
		$bu = 1;
		$bt = sprintf "%.0f", $bt;
	}

	if($bt != $gr[0])
	{
		$error_msg = $text{'error_msg_savegrace_failed_in_progress'};
		return $error_msg;
	}

	if($bu != $gr[1])
	{
		$error_msg = $text{'error_msg_savegrace_failed_in_progress'};
		return $error_msg;
	}
}

sub get_volume_dir()
{
	my @listmount, @voltab, @line, $i, $volume, $dir, %m;

	$volume = @_[0];
	
	@listmount = `$config{'mount'} 2>/dev/null`;

	open(VOLTAB, $config{'voltab'});
	@voltab = <VOLTAB>;
	close(VOLTAB);	

	for($i = 0; $i < @listmount; $i++)
	{
		chomp($listmount[$i]);
		@line = split(/\s+/, $listmount[$i]);
		
		if($line[0] =~ /\/dev\/mapper\/(\S+)-(\S+)/){
			$vg = $1;
			$lv = $2;
			$devtmp = "/dev/".$vg."/".$lv;
			$m{$devtmp} = $line[2];
		}
	}

	for($i = 0; $i < @voltab; $i++)
	{
		$voltab[$i] =~ s/#.*$//;
		if($voltab[$i])
		{
			@line = split(/\s+/, $voltab[$i]);
			if($line[0] eq $volume)
			{
				return $m{$line[1]};
			}
			
		}
	}
	return "vol_name_err";
}


1;
